package com.mhy.wxrobot

import com.mhy.wxrobot.message.*
import com.mhy.wxrobot.utils.Base64Utils
import com.mhy.wxrobot.utils.ImageBase64Md5

/**
 * Created By Mahongyin
 * Date    2021/1/9 21:43
 */
class Robot(token: String) {

    private val CHATBOT_WEBHOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=$token"

    @Throws(Exception::class)
    fun sendTextMsg(
        content: String,
        mobiles: List<String>,
        atAll: Boolean = false
    ): SendResult {
        val message = TextMessage(content)
        //@群内成员  手机号
        message.setMentionedMobileList(mobiles)
        message.setIsAtAll(atAll) //@所有人
        val result: SendResult = WxChatbotClient.send(CHATBOT_WEBHOOK, message)
        return result
    }

    @Throws(Exception::class)
    fun sendNewsMsg(
        title: String,
        content: String,
        url: String,
        picUrl: String
    ): SendResult {
        val article = NewsArticle()
        article.setTitle(title)
        article.setDescription(content)
        article.setUrl(url)
        article.setPicurl(picUrl)
        val message = NewsMessage()
        message.addNewsArticle(article)
        val result: SendResult = WxChatbotClient.send(CHATBOT_WEBHOOK, message)
        return result
    }

    @Throws(Exception::class)
    fun sendMarkdownMsg(markdownContent: String): SendResult {
        val message = MarkdownMessage()
        message.add(markdownContent)//直接原生的markdown 或者手动拼接
//        message.add(MarkdownMessage.getHeaderText(1, "header 1"))
//        message.add(MarkdownMessage.getHeaderText(2, "header 2"))
//        message.add(MarkdownMessage.getHeaderText(3, "header 3"))
//        message.add(MarkdownMessage.getHeaderText(4, "header 4"))
//        message.add(MarkdownMessage.getHeaderText(5, "header 5"))
//        message.add(MarkdownMessage.getHeaderText(6, "header 6"))
//        message.add(MarkdownMessage.getReferenceText("reference text"))
//        message.add("\n\n")
//        message.add("normal text")
//        message.add("\n\n")
//        message.add(MarkdownMessage.getBoldText("Bold Text"))
//        message.add("\n\n")
//        message.add(MarkdownMessage.getItalicText("Italic Text"))
//        message.add("\n\n")
//        val orderList = java.util.ArrayList<String>()
//        orderList.add("order item1")
//        orderList.add("order item2")
//        message.add(MarkdownMessage.getOrderListText(orderList))
//        message.add("\n\n")
//        val unorderList = java.util.ArrayList<String>()
//        unorderList.add("unorder item1")
//        unorderList.add("unorder item2")
//        message.add(MarkdownMessage.getUnorderListText(unorderList))
//        message.add("\n\n")
//        message.add(MarkdownMessage.getImageText("http://img01.taobaocdn.com/top/i1/LB1GCdYQXXXXXXtaFXXXXXXXXXX"))
//        message.add(
//            MarkdownMessage.getLinkText(
//                "This is a link",
//                "https://work.weixin.qq.com/api/doc#90000/90135/91760"
//            )
//        )
        val result: SendResult = WxChatbotClient.send(CHATBOT_WEBHOOK, message)
        return result
    }


    @Throws(Exception::class)
     fun sendImageMsg(imageUrlOrPath: String): SendResult {
        // val string = "https://c-ssl.duitang.com/uploads/blog/202008/22/20200822114634_2b3e0.thumb.1000_0.png"
        var image: ImageBase64Md5? = null
        if (imageUrlOrPath.startsWith("http")) {
            image = Base64Utils.ImageToBase64ByOnline(imageUrlOrPath)
        } else {
            image = Base64Utils.ImageToBase64ByLocal(imageUrlOrPath)
        }
        val imageMessage = ImageMessage(image?.getBase64(), image?.getMd5())
        val result: SendResult = WxChatbotClient.send(CHATBOT_WEBHOOK, imageMessage)
        return result
    }

     fun sendJson(json: String): SendResult {
        return WxChatbotClient.sendJson(CHATBOT_WEBHOOK, json)
    }
}